package net.huashitong.zhApp.controller;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.filter.EarlyWarningNodeFilter;
import net.huashitong.Project.service.EarlyWarningNodeService;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.node.filter.NodeNewschedulesFilter;
import net.huashitong.node.service.NodeNewschedulesService;
import net.huashitong.oversee.entity.ScoreSummary;
import net.huashitong.oversee.entity.SupervisionTask;
import net.huashitong.oversee.filter.CoordinateFilter;
import net.huashitong.oversee.filter.ScoreSummaryFilter;
import net.huashitong.oversee.filter.SupervisionTaskFilter;
import net.huashitong.oversee.service.CoordinateService;
import net.huashitong.oversee.service.ScoreSummaryService;
import net.huashitong.oversee.service.SupervisionTaskService;
import net.huashitong.taskGroup.entity.JobGroup;
import net.huashitong.taskGroup.service.JobGroupService;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.user.filter.BnpUserFilter;
import net.huashitong.user.service.BnpUserService;
import net.huashitong.user.service.LoginLogService;
import net.huashitong.zhApp.service.AppGradePointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/zhApp")
public class AppUserController extends BaseCtrl {

    @Autowired
    private BnpUserService bnpUserService;
    @Autowired
    private LoginLogService loginLogService;
    @Autowired
    private SupervisionTaskService taskService;
    @Autowired
    private CoordinateService coordinateService;
    @Autowired
    private NodeNewschedulesService nodeNewschedulesService;
    @Autowired
    private EarlyWarningNodeService earlyWarningNodeService;
    @Autowired
    private JobGroupService jobGroupService;
    @Autowired
    private AppGradePointService appGradePointService;
    @Autowired
    private NewProjectsService newProjectsService;
    @Autowired
    private ScoreSummaryService scoreSummaryService;

    /*
        1待办任务、2督查任务、3提请协调、4逾期预警、5督查督办、
        6绩效考核、7攻坚任务、8报表模块、9项目管理 、10 我的项目、
        11备件上传、12督查反馈、13前期项目、14节点审核、15项目概览、
        16预备项目、17在建项目、18竣工项目、19滞后项目、20暂停项目、
        21节点清单(审批部门才有)、22用户管理（只有管理员才有）、23模板管理
     */

    @ResponseBody
    @RequestMapping(value = "/queryByPhone", method = RequestMethod.GET)
    public Map<String, Object> queryUserByFilter(@RequestParam String phone) {
        BnpUserFilter bnpUserFilter = new BnpUserFilter();
        bnpUserFilter.setPhone(phone);
        List<BnpUser> userList = bnpUserService.queryByFilter(bnpUserFilter);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("list", userList);
        resMap.put("systemName", "重大项目节点管理系统");
        resMap.put("systemCode", "ZD");

        String role;
        String branchType;
        String systemRights = "";
        if (ObjectUtils.isNotBlank(userList) && userList.size() > 0) {
            BnpUser user = userList.get(0);
            branchType = user.getBranchType();
            role = user.getRole();
            String flag = branchType + role;
            switch (flag) {
                case "0经办人":    //承办单位经办人
                    systemRights = "1,3,7,4,10,11,12,2,6,15";
                    break;
                case "0责任人":    //承办单位责任人
//                    systemRights = "1,2,7,4,10,3,13,6";
                    systemRights = "1,3,7,4,10,11,12,2,6,15";
                    break;
                case "1经办人":    //审批部门经办人
                    systemRights = "1,3,7,4,10,14,5,2,6,21";
                    break;
                case "1责任人":    //审批部门责任人
//                    systemRights = "1,2,7,4,10,3,13,6";
                    systemRights = "1,3,7,4,10,14,5,2,6,21";
                    break;
                case "4经办人":    //督查组
                    systemRights = "1,2,3,4,5,6,7,10,15";
                    break;
                case "6领导":      //分管领导
//                    systemRights = "1,3,2,4,6,7,10,15";
                    systemRights = "15,6,7,3,16,13,17,18";
                    break;
                case "7领导":      //主要领导
                    systemRights = "15,6,7,16,13,17,18,19,20";
                    break;
                case "2经办人":    //管理员
                    systemRights = "1,2,3,4,6,7,8,9,15,22,23";
                    break;
                default:
                    systemRights = "";
                    break;
            }
        }

        resMap.put("systemRights", systemRights);
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功获取数据");
        result.put("data", resMap);
        return result;
    }


    /**
     * APP首页图标里面的数值
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryIconNum", method = RequestMethod.POST)
    public Map<String, Object> queryIconNum(@RequestBody BnpUserFilter filter) {
        BnpUser user = queryUserAuth(filter.getUserPhone());
        if (ObjectUtils.isBlank(user)) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", "202");
            result.put("message", "无用户权限");
            result.put("data", null);
            return result;
        } else {

            Map<String, Object> result = new HashMap<>();
            Map<String, Object> map = new HashMap<>();
            String branchType = user.getBranchType();
            String role = user.getRole();
            if (!("领导".equals(role) && ("6".equals(branchType) || "7".equals(branchType)))) {
                //领导
                //******************未完成的督查任务******************
                SupervisionTaskFilter taskFilter0 = new SupervisionTaskFilter();
                taskFilter0.setTaskResult("0");
                taskFilter0.setValid("1");
                taskFilter0.setUserinfo(user.getPhone());
                taskFilter0.setBranchType(user.getBranchType());
                taskFilter0.setRole(user.getRole());
                taskFilter0.setPaging(null);
                List<SupervisionTask> taskCount = taskService.getTaskLists(taskFilter0);
                int noFinishTask = taskCount.size();

                //记录登录日志
                loginLogService.doLoginLog(user);
                //******************我的督查******************
                SupervisionTaskFilter taskFilter = new SupervisionTaskFilter();
                taskFilter.setUserinfo(user.getPhone());
                taskFilter.setBranchType(user.getBranchType());
                taskFilter.setRole(user.getRole());
                taskFilter.setValid("1");
                int myTaskNum = taskService.getMissinTaskCount(taskFilter);
                //******************攻坚任务******************
                SupervisionTaskFilter taskFilter1 = new SupervisionTaskFilter();
                taskFilter1.setUserinfo(user.getPhone());
                taskFilter1.setBranchType(user.getBranchType());
                taskFilter1.setRole(user.getRole());
                taskFilter1.setValid("1");
                taskFilter1.setTackFlag("1");
                int myTackTaskNum = taskService.getMissinTaskCount(taskFilter1);
                //******************提请协调******************
                CoordinateFilter coordinateFilter = new CoordinateFilter();
                coordinateFilter.setUserinfo(user.getPhone());
                coordinateFilter.setBranchType(user.getBranchType()); //设置查询权限
                if (StringUtils.isBlank(coordinateFilter.getGroupCode())) //督查组权限
                {
                    coordinateFilter.setGroupCode(user.getGroupCode());
                }
                int questionNum = coordinateService.queryCoordinateCount(coordinateFilter);
                //******************待办任务******************
                NodeNewschedulesFilter filter1 = new NodeNewschedulesFilter();
                filter1.setOperatorUser(user.getName());
                filter1.setOperatorPhone(user.getPhone());
                //进行中的小节点个数
                int nodeNum = nodeNewschedulesService.queryListByFilterZong(filter1).size();

                //******************逾期预警 = 节点逾期 + 督查逾期******************
                int earlyWarningCount = 0;
                //1.节点逾期
                EarlyWarningNodeFilter earlyWarningNodeFilter = new EarlyWarningNodeFilter();
                if (!"2".equals(user.getBranchType())) {
                    earlyWarningNodeFilter.setOperatorUser(user.getName());
                }
                int earlyWarningNodeCount = earlyWarningNodeService.querywarningCount1(earlyWarningNodeFilter);
                //2.督查逾期
                SupervisionTaskFilter taskFilter2 = new SupervisionTaskFilter();
                taskFilter2.setTaskResult("0");
                taskFilter2.setValid("1");
                taskFilter2.setTasktimes("3");          //第三轮督查
                taskFilter2.setTaskResult("3");         //已考核
                taskFilter2.setUserinfo(user.getPhone());
                taskFilter2.setBranchType(user.getBranchType());
                taskFilter2.setRole(user.getRole());
                taskFilter2.setPaging(null);
                List<SupervisionTask> taskCount2 = taskService.getTaskLists(taskFilter2);
                int earlyWarningTask = taskCount2.size();
                earlyWarningCount = earlyWarningNodeCount + earlyWarningTask;


                Map<String, String> scoreAndRank;
                //用户得分
                List<JobGroup> jobGroups = jobGroupService.query();
                Map<String, String> jobGroupMap = new HashMap<>();
                for (JobGroup j : jobGroups) {
                    if (!jobGroupMap.containsKey(j.getGroupTel())) {
                        jobGroupMap.put(j.getGroupTel(), j.getGroupCode());
                    }
                }
                scoreAndRank = appGradePointService.qureyScoreAndRankByBnpUser(jobGroupMap, user);
                String itemRank = "";
                String finalScore = "";
                if (ObjectUtils.isNotBlank(scoreAndRank)) {
                    itemRank = scoreAndRank.get("itemRank");
                    if ("——".equals(itemRank)) {
                        itemRank = "-";
                    }
                    finalScore = scoreAndRank.get("finalScore");
                    if ("任务尚未完成，故无最终得分".equals(finalScore)) {
                        finalScore = "-";
                    }
                }
                map.put("itemRank", itemRank);
                map.put("finalScore", finalScore);
                map.put("myTaskNum", myTaskNum);                        //督查任务
                map.put("myTackTaskNum", myTackTaskNum);                //攻坚任务
                map.put("questionNum", questionNum);                    //提请协调
                map.put("nodeNum", nodeNum + noFinishTask);             //待办任务
                map.put("earlyWarning", earlyWarningCount);             //逾期预警
                map.put("isLeader", "0");                               //非领导
                result.put("data", map);
            } else if("领导".equals(role) && "6".equals(branchType)) {
                //县处级领导
                map = newProjectsService.getStarOrNotWorkMapByUser(null, user);
                //获取得分跟排名
                ScoreSummaryFilter summaryFilter =  new ScoreSummaryFilter();
                summaryFilter.setBranchType("6");
                summaryFilter.setUserId(user.getSysId());
                List<ScoreSummary> list = scoreSummaryService.queryByFilterNoPage(summaryFilter);
                if(ObjectUtils.isNotBlank(list)){
                    ScoreSummary scoreSummary = list.get(0);
                    map.put("itemRank", scoreSummary.getTypeRank());
                    map.put("finalScore", scoreSummary.getScore());
                }else{
                    map.put("itemRank", "-");
                    map.put("finalScore", "0");
                }

                map.put("year", "2019");
                map.put("isLeader", "2");
                result.put("data", map);
            }else{
                map = newProjectsService.getStarOrNotWorkMap(null);
                map.put("year", "2019");
                map.put("isLeader", "1");
                result.put("data", map);
            }
            result.put("code", "200");
            result.put("message", "获取成功");
            return result;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/queryUserDetailByUserPhone", method = RequestMethod.POST)
    public Map<String, Object> queryUserDetailByUserPhone(@RequestBody BnpUserFilter filter) {
        BnpUser user = queryUserAuth(filter.getUserPhone());
        Map<String, Object> result = new HashMap<>();
        if (ObjectUtils.isBlank(user)) {
            result.put("code", "202");
            result.put("message", "无用户权限");
            return result;
        }else{
            result.put("code", "200");
            result.put("message", "查询成功");
            result.put("userAuth", user);
            return result;
        }
    }

}
